﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Store
{
    public partial class 产品出库单管理 : System.Web.UI.Page
    {
        protected string Notice = "";
        string sql =
@"Select 
        ck.[ID],
        序号,
        合同编号,
        整车米数,
        车号,
        cp.[品名]+' '+cp.[钢级]+' '+cp.[规格] AS 品名,
        发往单位,
        整车捆数,
        整车吨位,
        整车支数,
        发运时间,
        是否合格,
        是否送检,
        库管员,
        case 
        when 生产线=101 then '一分厂' 
        when 生产线=201 then '二分厂B1' 
        when 生产线=202 then '二分厂B2' 
        when 生产线=501 then '五分厂C1' 
        when 生产线=502 then '五分厂C2' 
        else '其他' 
        end as 生产线

from [产品出库清单] AS ck INNER JOIN
dbo.产品 as cp ON 
ck.产品类别ID = cp.ID";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if ((object)Session["OperatorDCode"] != null)
                {
                    string dcode = (string)Session["OperatorDCode"];
                    if (dcode != "3505") this.Button_Pass.Visible = false;
                    if (dcode == "3506")
                    {
                        this.XianShi.Enabled = false;
                        this.XianShi.Text = "3";
                    }
                }

                this.DingHuo.DataBind();
                this.Button_Query_Click(null, null);
            }
            else
            {
                this.SqlDataSource1.SelectCommand = this.SqlText.Text;
            }
        }
        /// <summary>
        /// 通用例程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.GridView1.SelectedIndex = -1;
            this.Button_Edit.Enabled = false;
            this.Button_Deliver.Enabled = false;
            this.Button_Pass.Enabled = false;
            this.Button_Delete.Enabled = false;

            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        /// <summary>
        /// 通用例程
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }

        /// <summary>
        /// 翻页处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
        protected void SqlDataSource1_Selected(object sender, SqlDataSourceStatusEventArgs e)
        {
            Label_Total.Text = "共查询到 " + e.AffectedRows + " 项记录";
        }
        protected void PageSizeOption_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.GridView1.PageSize = int.Parse(this.PageSizeOption.Text);
        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Button_Delete.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Pass.CommandName = this.GridView1.SelectedDataKey[0].ToString();
            this.Button_Deliver.CommandName = this.GridView1.SelectedDataKey[0].ToString();

            if ((bool)this.GridView1.SelectedDataKey[1])
            {
                this.Button_Delete.Enabled = false;
                this.Button_Pass.Enabled = true;

                this.Button_Deliver.Text = "取消送检";
                this.Button_Deliver.CommandArgument = "false";

                if ((bool)this.GridView1.SelectedDataKey[2])
                {
                    this.Button_Pass.Text = "取消盖章";
                    this.Button_Pass.CommandArgument = "false";

                    this.Button_Deliver.Enabled = false;
                }
                else
                {
                    this.Button_Pass.Text = "盖 章";
                    
                    this.Button_Pass.CommandArgument = "true";
                    this.Button_Deliver.Enabled = true;
                }

            }
            else
            {
                this.Button_Deliver.Text = "送 检";
                this.Button_Deliver.CommandArgument = "true";
                this.Button_Delete.Enabled = true;
                this.Button_Deliver.Enabled = true;
                this.Button_Pass.Text = "盖 章";
                this.Button_Pass.CommandArgument = "true";
                this.Button_Pass.Enabled = false;
            }

            this.Button_Edit.Enabled = true;

            this.Button_Edit.OnClientClick = "parent.swin_load('/Store/产品出库单录入.aspx?id="
                + this.GridView1.SelectedDataKey[0].ToString() + "');return false;";

        }

        /// <summary>
        /// 得到查询序列的状态
        /// </summary>
        /// <returns></returns>
        private string GetState()
        {
            string state = "";
            int xuhao;
            if (int.TryParse(this.XuHao.Text, out xuhao))
            {
                state += " and ck.[序号]=" + xuhao;
            }

            if (this.XianShi.Text == "1")
            {
                state += " and ck.[是否送检]=0";
            }
            else if (this.XianShi.Text == "2")
            {
                state += " and ck.[是否送检]=1 and ck.[是否合格]=0";
            }
            else if (this.XianShi.Text == "3")
            {
                state += " and ck.[是否合格]=1";
            }

            //产品的读取较为特殊
            if (this.ChanPin.Text != "")
            {
                sql += " and ck.[产品类别ID]=" + this.ChanPin.Text;
            }
            else
            {
                //如果什么都没有选择，则默认为1
                sql += " and ck.[产品类别ID]=1 ";
            }
            if (this.DingHuo.Text != "0")
            {
                state += " and ck.[订货单位ID] =" + this.DingHuo.Text;
            }
            this.HeTong.Text = this.HeTong.Text.Trim();
            this.HeTong.Text = this.HeTong.Text.Replace("[", "");

            if (this.HeTong.Text != "")
            {
                state += " and ck.[合同编号] like '%" + this.HeTong.Text + "%'";
            }

            if (this.FenChang.Text != "0")
            {
                state += " and ck.[生产线] =" + this.FenChang.Text;
            }

            DateTime d;
            if (DateTime.TryParse(this.Date1.Text, out d))
            {
                state += " and ck.[发运时间]>='" + d.ToShortDateString() + "'";
            }
            if (DateTime.TryParse(this.Date2.Text, out d))
            {
                state += " and ck.[发运时间]<='" + d.ToShortDateString() + "'";
            }

            if (state.StartsWith(" and"))
            {
                state = state.Substring(4);
            }
            if (state != "")
            {
                state = " where " + state;
            }
            return state;
        }
        protected void Button_Query_Click(object sender, EventArgs e)
        {
            string state = this.GetState();
            this.SqlText.Text = sql + state + " order by ck.[ID] desc";
            this.SqlDataSource1.SelectCommand = "";
            this.SqlDataSource1.SelectCommand = this.SqlText.Text;
        }

        protected void Button_Reset_Click(object sender, EventArgs e)
        {
            this.ChanPin.Text = "0";
            this.FenChang.Text = "0";
            this.DingHuo.Text = "0";
            this.HeTong.Text = "";
            this.Date1.Text = "";
            this.Date2.Text = "";
            this.XuHao.Text = "";
            this.XianShi.Text = "0";
        }

        protected void Button_Delete_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.DeleteParameters["ID"].DefaultValue = this.Button_Delete.CommandName;
            if (this.SqlDataSource1.Delete() > 0)
            {
                this.Notice = "·删除成功";
            }
            else
            {
                this.Notice = "·删除失败";
            }
        }

        protected void Button_Deliver_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.InsertParameters["ID"].DefaultValue = this.Button_Deliver.CommandName;
            this.SqlDataSource1.InsertParameters["VAL"].DefaultValue = this.Button_Deliver.CommandArgument;
            if (this.SqlDataSource1.Insert() > 0)
            {
                this.Notice = "·"+this.Button_Deliver.Text.Replace(" ","")+"成功";
            }
            else
            {
                this.Notice = "·" + this.Button_Deliver.Text.Replace(" ", "") + "失败";
            }
        }

        protected void Button_Pass_Click(object sender, EventArgs e)
        {
            this.SqlDataSource1.UpdateParameters["ID"].DefaultValue = this.Button_Pass.CommandName;
            this.SqlDataSource1.UpdateParameters["VAL"].DefaultValue = this.Button_Pass.CommandArgument;
            if (this.SqlDataSource1.Update() > 0)
            {
                this.Notice = "·" + this.Button_Pass.Text.Replace(" ", "") + "成功";
            }
            else
            {
                this.Notice = "·" + this.Button_Pass.Text.Replace(" ", "") + "失败";
            }
        }
    }
}
